#-*- coding: UTF-8 -*-
__author__ = 'babycooler'
from PIL import Image
from glob import glob
import sys,os,argparse
def thumbnailer(path,proportion):
    imgs = []
    path = str(path)
    if not path.endswith(os.path.sep):
        path+=os.path.sep
    imgs = glob(path+r'*.jpg')
    imgs.extend(glob(path+r'*.png'))
    if len(imgs)>0:
        if not os.path.isdir(path+'result'):os.mkdir(path+'result')
        for item in imgs:
            img = Image.open(item)
            w,h = img.size
            w = int(w*float(proportion))
            h = int(h*float(proportion))
            img.thumbnail((w,h))
            img.save(path+'result'+os.path.sep+os.path.basename(item))
        print "files have been saved successfully"
    else:
        print "not images found in this dir,please try again"

if __name__=='__main__':
    args = argparse.ArgumentParser(description='simple tool for generating thumbnail images')
    args.add_argument('path',type=str,help='please type the dir path where the images are located')
    args.add_argument('-proportion',type=float,help='please type the proportion that you want the images to be resized to',default=0.3)
    arg = args.parse_args()
    thumbnailer(arg.path,arg.proportion)
